package com.teamone.etl.apimanager.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.teamone.etl.apimanager.entity.ApiRule;
import com.teamone.etl.apimanager.request.BaseQuery;
import com.teamone.etl.apimanager.service.ApiRuleService;
import com.teamone.etl.apimanager.valid.AddGroup;
import com.teamone.etl.apimanager.valid.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 接口规则表(ApiRule)表控制层
 *
 * @author makejava
 * @since 2021-01-20 18:32:05
 */
@RestController
@RequestMapping("apiRule")
public class ApiRuleController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ApiRuleService apiRuleService;

    /**
     * 查询接口规则列表
     * @param baseQuery 分页对象
     * @return 接口规则列表
     */
    @PostMapping
    public R selectAll(@RequestBody BaseQuery baseQuery){
        return success(apiRuleService.selectAll(baseQuery));
    }

    /**
     *根据接口规则id删除单条记录
     * @param id 结构规则id
     * @return 删除规则
     */
    @DeleteMapping("{id}")
    public R logicDeleteById(@PathVariable Integer id){
        apiRuleService.logicDeleteById(id);
        return success(true);
    }

    /**
     * 根据id修改状态
     * @param id 规则id
     * @param state 状态值
     * @return 修改状态成功/失败
     */
    @PutMapping("changeState/{id}/{state}")
    public R changeState(@PathVariable Integer id,@PathVariable Integer state){
        apiRuleService.changeState(id,state);
        return success(true);
    }

    /**
     * 新增接口规则
     * @param apiRule 新增对象
     * @return 新增结果
     */
    @PostMapping("save")
    public R saveOne(@RequestBody @Validated(AddGroup.class) ApiRule apiRule){
        apiRuleService.saveOne(apiRule);
        return success(true);
    }

    /**
     * 修改接口规则
     * @param apiRule 修改对象
     * @return 修改结果
     */
    @PutMapping
    public R updateOne(@RequestBody @Validated(UpdateGroup.class) ApiRule apiRule){
        apiRuleService.updateOne(apiRule);
        return success(true);
    }
}